package com.meihua.shellapp.base;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;

import com.aspsine.irecyclerview.IRecyclerView;
import com.aspsine.irecyclerview.OnLoadMoreListener;
import com.aspsine.irecyclerview.OnRefreshListener;
import com.meihua.shellapp.R;
import com.meihua.shellapp.dialog.LoadingDialog;
import com.meihua.shellapp.view.StatusBarCompat;
import com.meihua.shellapp.view.footer.LoadMoreFooterView;

import butterknife.ButterKnife;

/**
 * 作者：lc on 2017/9/26.
 * 邮箱：lc0607live@163.com
 */

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener, OnLoadMoreListener, OnRefreshListener {
    public Activity mContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setBackgroundDrawable(getResources().getDrawable(R.color.colorAppActivityBg));
        doBeforeSetcontentView();
        setContentView(getLayoutId());
        MyApplication.getInstance().addActivity(this);
        ButterKnife.bind(this);
        mContext = this;
        this.initView(savedInstanceState);
        createLoadData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        resumeLoadData();
    }

    /**
     * 每次回到activity加载数据
     */
    public void resumeLoadData() {
    }

    /**
     * 创建activity时加载一次数据
     */
    public void createLoadData() {
    }

    /**
     * 隐藏软键盘
     */
    public void hiddenInputMethod() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            //隐藏
            imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
        }
    }

    /**
     * 显示软键盘
     *
     * @param view
     */
    public void showInputMethod(final View view) {
        getWindow().getDecorView().postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    view.requestFocus();
                    //显示
                    imm.showSoftInput(view, 0);
                }
            }
        }, 100);
    }
    private LoadingDialog loadingDialog;
    /**
     * 显示load框
     */
    public void showLoadingDialog() {
        if (loadingDialog == null){
            loadingDialog = new LoadingDialog();
        }
        if (!loadingDialog.isVisible()) {
            loadingDialog.show(getSupportFragmentManager(), getClass().getSimpleName());
        }
    }
    /**
     * 隐藏load框
     */
    public void dismissLoadingDialog() {
        if (loadingDialog == null) {
            return;
        }
        loadingDialog.dismiss();
        loadingDialog = null;
    }

    @Override
    protected void onDestroy() {
        loadingDialog = null;
        super.onDestroy();
    }

    /**
     * 获取屏幕分辨率
     *
     * @return
     */
    public int[] getWH() {
        if (MyApplication.getInstance().mWh[0] == 0) {
            Display display = getWindowManager().getDefaultDisplay(); //Activity#getWindowManager()
            Point size = new Point();
            display.getSize(size);
            MyApplication.getInstance().mWh[0] = size.x;
            MyApplication.getInstance().mWh[1] = size.y;
        }
        return MyApplication.getInstance().mWh;
    }

    /**
     * 设置layout前配置
     */
    public void doBeforeSetcontentView() {
        // 无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 设置竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        // 默认着色状态栏
        SetStatusBarColor();
    }

    /*********************子类实现*****************************/
    //获取布局文件
    public abstract int getLayoutId();

    //初始化view
    public abstract void initView(Bundle savedInstanceState);

    /**
     * 着色状态栏（4.4以上系统有效）
     */
    protected void SetStatusBarColor() {
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorAppTitleBg));
    }

    /**
     * 着色状态栏（4.4以上系统有效）
     */
    protected void SetStatusBarColor(int color) {
        StatusBarCompat.setStatusBarColor(this, color);
    }

    /**
     * 沉浸状态栏（4.4以上系统有效）
     */
    protected void SetTranslanteBar() {
        StatusBarCompat.translucentStatusBar(this);
    }

    protected void setNoticeTitleBg(int color) {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        ViewGroup mContentView = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
        View statusBarView = mContentView.getChildAt(0);
        //移除假的 View
        if (statusBarView != null && statusBarView.getLayoutParams() != null && statusBarView.getLayoutParams().height == MyApplication.getInstance().getStatusBarHeight(this)) {
            mContentView.removeView(statusBarView);
        }
        //不预留空间
        if (mContentView.getChildAt(0) != null) {
            ViewCompat.setFitsSystemWindows(mContentView.getChildAt(0), false);
        }
        if (statusBarView != null && statusBarView.getLayoutParams() != null && statusBarView.getLayoutParams().height == MyApplication.getInstance().getStatusBarHeight(this)) {
            //避免重复调用时多次添加 View
            statusBarView.setBackgroundColor(getResources().getColor(color));
            return;
        }
        statusBarView = new View(this);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, MyApplication.getInstance().getStatusBarHeight(this));
        statusBarView.setBackgroundColor(getResources().getColor(color));
        //向 ContentView 中添加假 View
        mContentView.addView(statusBarView, 0, lp);
    }

    protected void setNoticeTitleBg() {
        Window window = getWindow();
        ViewGroup mContentView = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);

        //首先使 ChildView 不预留空间
        View mChildView = mContentView.getChildAt(0);
        if (mChildView != null) {
            ViewCompat.setFitsSystemWindows(mChildView, false);
        }

        int statusBarHeight = MyApplication.getInstance().getStatusBarHeight(this);
        //需要设置这个 flag 才能设置状态栏
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //避免多次调用该方法时,多次移除了 View
        if (mChildView != null && mChildView.getLayoutParams() != null && mChildView.getLayoutParams().height == statusBarHeight) {
            //移除假的 View.
            mContentView.removeView(mChildView);
            mChildView = mContentView.getChildAt(0);
        }
        if (mChildView != null) {
            FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mChildView.getLayoutParams();
            //清除 ChildView 的 marginTop 属性
            if (lp != null && lp.topMargin >= statusBarHeight) {
                lp.topMargin -= statusBarHeight;
                mChildView.setLayoutParams(lp);
            }
        }
    }

    @Override
    public void onClick(View view) {
        hiddenInputMethod();
        switch (view.getId()) {
            case R.id.iv_top_left://返回
                finish();
                break;
        }
    }

    @Override
    public void onLoadMore() {
    }


    @Override
    public void onRefresh() {

    }

    public void closeRefreshing(IRecyclerView view) {
        view.setRefreshing(false);
        ((LoadMoreFooterView) view.getLoadMoreFooterView()).setStatus(LoadMoreFooterView.Status.GONE);
    }

    public void endRefreshing(IRecyclerView view) {
        ((LoadMoreFooterView) view.getLoadMoreFooterView()).setStatus(LoadMoreFooterView.Status.THE_END);
    }
}
